<?php


namespace app\web\service;


use app\common\lib\astrolog\Astrolog;
use app\common\model\AppSignText;
use app\common\model\AstroSignText;

class AstroExplain
{
    /**
     * 获取星盘解读数据
     * @param $recodeInfo
     * @return array
     */
    public function getAstroExplain($recodeInfo)
    {
        $natalData = Astrolog::getInstance()->setRecode1($recodeInfo)
            ->setExecPrefix(config('astrolog.prefix'))
            ->setIsAstrolabe(false)
            ->setUserSetting(config('astroset.setting1'))
            ->setGetDatas(['natal'])
            ->getAstroDatas()
            ->getDatas('natal');

        //落座全部词条
        $sign_text = (new AppSignText())->getAllSignText();
        //落宫全部词条
        $house_text = (new AstroSignText())->getAllHouseText();

        // 获取行星配置信息
        $planet_info = config('astro_new');
        $planet_name = $planet_info['planet_name'];
        $planet_en_icon = $planet_info['planet_en_icon'];
        $sign_name = $planet_info['sign_name'];
        $sign_icon = $planet_info['astro_icon'];
        $hourse_icon = $planet_info['house_icon'];
        $sign_abbreviation = $planet_info['sign_abbreviation'];
        //获取行星落座词条
        foreach ($natalData['planets'] as $k => $v)
        {
            //内行星落座信息
            if (in_array($v['id'] * 1, [0, 1, 2, 3, 4, 17]))
            {
                // 获取行星信息
                //行星id
                $array['planet']['id'] = $v['id'];
                //行星名称
                $array['planet']['name'] = $planet_name[$v['id']];
                //行星英文图标
                $array['planet']['en_icon'] = $planet_en_icon[$v['id']];
                //星座名称
                $array['sign']['name'] = $sign_name[$v['in_sign_id']];
                //星座缩写
                $array['sign']['abbreviation'] = $sign_abbreviation[$v['in_sign_id']];
                //星座背景字母图片地址
                $array['sign']['sign_icon'] = $sign_icon[$v['in_sign_id']];
                //日月升星座id
                $array['sign']['sign_id'] = $v['in_sign_id'];
                // 获取行星落座词条
                $sign_data = [];
                $recodeSex = (int)$recodeInfo['type'] === 1 ? 1 : 0;
                foreach ($sign_text as $sign)
                {
                    $sign['sex'] = (int)$sign['sex'];
                    if ($v['in_sign_id'] == $sign['sign_id'] && $v['id'] == $sign['planet_id'] && ($sign['sex'] === 99 || $sign['sex'] === $recodeSex))
                    {
                        $sign_data[] = $sign['text'];
                    }
                }
                $array['sign']['text'] = $sign_data;
            } elseif (in_array($v['id'] * 1, [5, 6, 7, 8, 9]))
            {
                //外行星落宫信息
                //行星id
                $array['planet']['id'] = $v['id'];
                //行星名称
                $array['planet']['name'] = $planet_name[$v['id']];
                //宫位名称
                $array['sign']['name'] = $v['in_house_id'] . '宫';
                $array['sign']['sign_icon'] = $hourse_icon[$v['in_house_id']];
                //行星落宫词条
                $house_data = [];
                foreach ($house_text as $house)
                {
                    if ($v['in_house_id'] == $house['tid'] && $v['id'] == $house['planet'])
                    {

                        $last_str = mb_substr($house['text'], -1);
                        if ($last_str == '。')
                        {
                            $house['text'] = mb_substr($house['text'], 0, -1) . '；';
                        } elseif (mb_substr($house['text'], -1) != '；')
                        {
                            $house['text'] .= '；';
                        }
                        $house_data[] = $house['text'];
                    }
                }
                $array['sign']['text'] = $house_data;
            } else
            {
                continue;
            }
            $data[] = $array;
        }
        $data = super_unique($data, true);

        //整理合并词条
        foreach ($data as $key => $value)
        {
            $data[$key]['sign']['sing_text'] = '';
            foreach ($value['sign']['text'] as $k => $v)
            {
                $data[$key]['sign']['sing_text'] .= $v;
            }
        }
        //调整内外行星显示顺序
        foreach ($data as $key => $value)
        {
            if ($key * 1 >= 2)
            {
                $key++;
                $data[$key] = $value;
            }
            if ($key * 1 === 11)
            {
                $key = 2;
                $data[$key] = $value;
            }
        }
        unset($data[11]);

        //日月升简介数据
        $smrData = [];
        $smrData[1]['sunIntro'] = $this->returnIntro(0, $data[0]['sign']['sign_id']);
        $smrData[2]['moonIntro'] = $this->returnIntro(1, $data[1]['sign']['sign_id']);
        $smrData[0]['riseIntro'] = $this->returnIntro(17, $data[2]['sign']['sign_id']);

        return [
            'smrData' => $smrData,
            'data' => $data
        ];
    }

    /**
     * @param $pid 行星id
     * @param $sid 星座id
     * 日月升简介
     */
    public function returnIntro($pid, $sid)
    {
        //日月升标题介绍数据
        $intro = [
            //日
            0 =>
                [
                    1 =>
                        [
                            0 => '勇敢的心',
                        ],
                    2 =>
                        [
                            0 => '现实主义',
                        ],
                    3 =>
                        [
                            0 => '文武双全',
                        ],
                    4 =>
                        [
                            0 => '死宅达人',

                        ],
                    5 =>
                        [
                            0 => '真·最强王者',
                        ],
                    6 =>
                        [
                            0 => '追求完美',
                        ],
                    7 =>
                        [
                            0 => '恋爱圣手',
                        ],
                    8 =>
                        [
                            0 => '绝对占有',
                        ],
                    9 =>
                        [
                            0 => '自由不羁',
                        ],
                    10 =>
                        [
                            0 => '闷骚内敛',
                        ],
                    11 =>
                        [
                            0 => '外星生物',

                        ],
                    12 =>
                        [
                            0 => '多愁善感',
                        ],
                ],
            //月
            1 =>
                [
                    1 =>
                        [
                            0 => '简单率真',
                        ],
                    2 =>
                        [
                            0 => '宠辱不惊',
                        ],
                    3 =>
                        [
                            0 => '善变大咖',
                        ],
                    4 =>
                        [
                            0 => '敏感多情',
                        ],
                    5 =>
                        [
                            0 => '唯我独尊',
                        ],
                    6 =>
                        [
                            0 => '崇尚理性',
                        ],
                    7 =>
                        [
                            0 => '纠结拖延',
                        ],
                    8 =>
                        [
                            0 => '酸醋坛子',
                        ],
                    9 =>
                        [
                            0 => '放飞自我',
                        ],
                    10 =>
                        [
                            0 => '踏实刻苦',
                        ],
                    11 =>
                        [
                            0 => '追求真理',
                        ],
                    12 =>
                        [
                            0 => '世间皆苦',
                        ],
                ],
            //升
            17 =>
                [
                    1 =>
                        [
                            0 => '勇往直前',
                        ],
                    2 =>
                        [
                            0 => '美食大咖',
                        ],
                    3 =>
                        [
                            0 => '变化多端',
                        ],
                    4 =>
                        [
                            0 => '温柔敏感',
                        ],
                    5 =>
                        [
                            0 => '霸气外放',
                        ],
                    6 =>
                        [
                            0 => '完美主义',
                        ],
                    7 =>
                        [
                            0 => '优雅随和',
                        ],
                    8 =>
                        [
                            0 => '冷酷神秘',
                        ],
                    9 =>
                        [
                            0 => '乐观开朗',
                        ],
                    10 =>
                        [
                            0 => '勤恳高尚',
                        ],
                    11 =>
                        [
                            0 => '疏离淡然',
                        ],
                    12 =>
                        [
                            0 => '百变达人 ',
                        ],
                ],
        ];
        return $res = $intro[$pid][$sid][0];
    }
}
